<?php

session_start();

$library = $_GET['library'];
$id = $_GET['id'];
$part = $_GET['part'];
$type = $_GET['type'];
if (!$part) $part = 1;

$config = sqlite_open("config.db");
$gen = sqlite_fetch_array(sqlite_query($config, "SELECT * FROM general"));
$lib = sqlite_fetch_array(sqlite_query($config, "SELECT * FROM library WHERE id='$library'"));

if ($_GET['go']) {
  $keep = 1;
  $t = count($_SESSION['queue']);
  for ($i=0; $i < $t; $i++) {
    if ($i == $_SESSION['playing']) $current = $i;
  }

  if ($_GET['go'] == "next") {
    if ($current == ($t-1)) {
      $next = $_SESSION['queue'][0];
      $_SESSION['playing'] = 0;
    }
    else {
      $_SESSION['playing'] = ($current + 1);
      $next = $_SESSION['queue'][($current + 1)];
    }
    $id = $next;
  }
  elseif ($_GET['go'] == "prev") {
    if ($current == 0) {
      $prev = $_SESSION['queue'][($t - 2)];
      $_SESSION['playing'] = ($t - 2);
    }
    elseif ($current == 1) {
      $prev = $_SESSION['queue'][($t - 1)];
      $_SESSION['playing'] = ($t - 1);
    }
    else {
      $_SESSION['playing'] = ($current - 2);
      $prev = $_SESSION['queue'][($current - 2)];
    }
    $id = $prev;
  }
  elseif ($_GET['go'] == "ss") {
    $_SESSION['playing'] = 0;
    $id = $_SESSION['queue'][0];
  }
  else {
    $_SESSION['playing'] = $_GET['go'];
    $id = $_SESSION['queue'][$_GET['go']];
  }
}

if ($lib['streaming'] == "1") {
  $db = sqlite_open($gen['path'] . "\\library\\". $lib['file']);
  $sql = sqlite_query($db, "SELECT * FROM items WHERE item_id = '$id'");
  $data = sqlite_fetch_array($sql);
  $filename = $data['item_location'];
  if ($_GET['type'] == "movie") {
    if (substr($filename,0,1) == "|") $filename = substr($filename,1,strlen($filename));
    if (substr($filename,-1,1) == "|") $filename = substr($filename,0,strlen($filename) - 1);
    $filename = split("\|", $filename);
    $files = count($filename);

    if ($files > 1) {
      $file = $lib['umedia_folder']."\\".str_replace(strtolower($lib['path']), "", strtolower($filename[($part - 1)]));
      $next = ($part + 1);
      $prev = ($part - 1);
    }
    else {
      $file = $lib['umedia_folder']."\\".str_replace(strtolower($lib['path']), "", strtolower($filename[0]));
    }
  }
  
  if ($_GET['type'] == "music") {
    $file = $lib['umedia_folder']."\\".str_replace(strtolower($lib['path']), "", strtolower($filename));
    if (!$keep) {
      unset($_SESSION['queue']);
      $_SESSION['queue'][0] = $id;
      $_SESSION['playing'] = 0;
    }
  }
  $file = addslashes($file);
}
else exit();

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title><?php echo $data['item_name']?></title>
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<style type="text/css">
a {color: <?php echo $gen['fncolor']?>}
td {font-size: 14}
</style>

<script id="clientEventHandlersJS" language="javascript">
<!--

function OnLoad()
{
	document.UPlayer.UseTCP();
	document.UPlayer.UseMediaServer("<?php echo $gen['host'];?>:<?php echo $gen['streamport'];?>");
	document.UPlayer.EnableFlowControl(true, true);
	document.UPlayer.UseFile("<?php echo $file;?>");
	document.UPlayer.Play();
}

-->
</script>

<script language="javascript" for = UPlayer event = "OnStart()">
<!--

-->
</script>

<script language="javascript" for = UPlayer event = "OnStop()">
<!--
    <?php
    if ($type == "music") {
      echo "document.location.href = \"player.php?library=$library&go=next&type=$type\"";
    }
    elseif ($type == "movie") {
      if ($next <= $files) echo "document.location.href = \"player.php?library=$library&id=$id&part=$next&type=movie\"";
    }
	?>
-->
</script>

<script language="javascript" for = UPlayer event = "OnResize(X, Y)">
<!--
	document.UPlayer.width  = X;
    document.UPlayer.height = Y;
-->
</script>

</head>

<body onLoad="OnLoad()" bgcolor="<?php echo $gen['bgcolor']?>" style="font-family: Verdana; color: <?php echo $gen['fncolor']?>;">

<?php

echo "<table width=\"600\" align=\"center\">";

if ($_GET['type'] == "movie") {
  echo "<tr>";
  echo "<td align=\"center\">";
  echo "<OBJECT id=\"UPlayer\"
        width=\"1\"
        height=\"1\"
        codeBase=\"extra/UMediaControl4.cab\"
    	classid=\"clsid:CA11EB7C-1C85-4577-8A49-9E28EFB30184\" VIEWASTEXT>
        </OBJECT>";
  if ($files > 1) {
    if ($prev > 0) echo "<a href=\"player.php?library=$library&id=$id&part=$prev&type=movie\">";
    echo "Previous";
    if ($prev > 0) echo "</a>";
    echo "&nbsp;&nbsp;&nbsp;-&nbsp;&nbsp;&nbsp;";
    if ($next <= $files) echo "<a href=\"player.php?library=$library&id=$id&part=$next&type=movie\">";
    echo "Next";
    if ($next <= $files) echo "</a>";
  }
  echo "</td>";
  echo "</tr>";
}

if ($_GET['type'] == "music") {
  echo "<tr>";
  echo "<td style=\"border: 1px solid black;\">";

  $sql_tags = sqlite_query($db, "SELECT * FROM tags");
  while ($tags = sqlite_fetch_array($sql_tags)) {
    if ($tags['tag_name'] == "Genre")     $tag_genre = $tags['tag_col'];
    if ($tags['tag_name'] == "Album")     $tag_album = $tags['tag_col'];
    if ($tags['tag_name'] == "Artist")    $tag_artist = $tags['tag_col'];
    if ($tags['tag_name'] == "Track")     $tag_track = $tags['tag_col'];
  }

  echo "<center><b>Now Playing:</b></center>";
  echo "Artist: <b>".$data[$tag_artist]."</b><br>";
  echo "Song: <b>".$data['item_name']."</b><br>";
  echo "Album: <b>".$data[$tag_album]."</b><br>";
  echo "<br>";
  echo "<center>";

  echo '<OBJECT id="UPlayer"
                height="240"
                width="320"
                classid="clsid:CA11EB7C-1C85-4577-8A49-9E28EFB30184"
                CODEBASE="extra/UMediaControl4.cab">
			</OBJECT>';
  echo "<br>";
  echo "<button onclick=\"location.href = 'player.php?library=$library&go=prev&type=music'\">";
  echo "|&lt;&lt;</button>";
  echo "<button onclick=\"document.UPlayer.Stop()\">";
  echo "&gt;&gt;|</button>";
  echo "</center>";
  echo "</td>";
  echo "<td align=\"center\" width=\"400\" style=\"border: 1px solid black; padding: 20px;\">";
  if ($data['item_image'] != NULL) echo "<img src=\"thumb.php?id=$id&library=$library&w=300&h=250\"><br>";
  echo "</td>";
  echo "</tr>";
  echo "<tr>";
  echo "<td colspan=\"2\">";
  echo "<iframe name=\"queue\" src=\"queue.php?library=$library\" width=\"100%\" height=\"400\" style=\"border: 1px solid black;\">";
  echo "</iframe>";
  echo "</td>";
  echo "</tr>";
}
echo "</table>";
?>
</body>
</html>